﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class CurrencyController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly ICurrencyRepository CurrencyRepository;
        private readonly IUOMRepository uomRepository;
        public CurrencyController(ICommandBus commandBus, ICurrencyRepository CurrencyRepository, IUOMRepository uomRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.CurrencyRepository = CurrencyRepository;
            this.uomRepository = uomRepository;
        }
        //
        // GET: /Currency/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = CurrencyRepository.GetAll();
            ViewData["UOM"] = uomRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save2(int ID, string Code, string Descripttion, double ForeignExchange, double HomeExchange, bool IsDefault, string Name, int Status, string Symbol, int UOMID)
        {
            var form = new CurrencyFormModels()
            {
                ID = ID,
                Code=Code,
                Descripttion = Descripttion,
                ForeignExchange = ForeignExchange,
                HomeExchange = HomeExchange,
                IsDefault = IsDefault,
                Name = Name,
                Status = Status,
                Symbol = Symbol,
                UOMID=UOMID
            };
            var command = Mapper.Map<CurrencyFormModels, CreateOrUpdateCurrencyCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = CurrencyRepository.GetAll();
            return PartialView("_CurrencyList", model.ToPagedList(1, 10));
        }
        [HttpPost]
        public ActionResult Save(CurrencyFormModels frm)
        {
            var form = new CurrencyFormModels()
            {
                ID = frm.ID,
                Code = frm.Code,
                Descripttion = frm.Descripttion,
                ForeignExchange = frm.ForeignExchange,
                HomeExchange = frm.HomeExchange,
                IsDefault = frm.IsDefault,
                Name = frm.Name,
                Status = frm.Status,
                Symbol = frm.Symbol,
                UOMID=frm.UOMID
            };
            var command = Mapper.Map<CurrencyFormModels, CreateOrUpdateCurrencyCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = CurrencyRepository.GetAll();
            return PartialView("_CurrencyList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            Currency product = CurrencyRepository.GetById(ID);
            var obj = Mapper.Map<Currency, CurrencyFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteCurrencyCommand> list = new List<DeleteCurrencyCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteCurrencyCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = CurrencyRepository.GetAll();
            return PartialView("_CurrencyList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<Currency> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = CurrencyRepository.GetAll();
            }
            else
            {
                model = CurrencyRepository.GetMany(x => (x.Name.Contains(Name)) || (x.Descripttion.Contains(Name)) || (x.Code.Contains(Name)));

            }
            return PartialView("_CurrencyList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = CurrencyRepository.GetAll();
            return PartialView("_CurrencyList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
